package com.eightbears.beboy.ui.user.activity;

import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.banner.LocalImageHolderView;
import com.eightbears.beboy.entity.ProductEntity;
import com.eightbears.beboy.entity.ProductsEntity;
import com.eightbears.beboy.entity.status.Status_Error;
import com.eightbears.beboy.entity.status.Status_Product;
import com.eightbears.beboy.ui.user.fragment.ShopFragment;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_PRODUCT;

/**
 * 商城商品详细页面
 */
public class CommodityDetailActivity extends BaseActivity implements OnItemClickListener {

    public static final String PARAMS_SHOP_COMMODITY = "commodity";

    @BindView(R.id.convenientBanner)
    ConvenientBanner mConvenientBanner;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_point)
    TextView tv_point;
    @BindView(R.id.tv_product_title)
    TextView tv_product_title;
    @BindView(R.id.tv_validity)
    TextView tv_validity;
    @BindView(R.id.wv_content)
    WebView wv_content;

    private ProductsEntity productsEntity;
    private ProductEntity productEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cmmodity_detail);
        //侧滑删除实力化
        SwipeBackLayout swipeBackLayoutTwo = (SwipeBackLayout) findViewById(R.id.sw_shopdetail);
        swipeBackLayoutTwo.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        productsEntity = (ProductsEntity) getIntent().getExtras().get(ShopFragment.PARAMS_PRODUCTS_ITEM);
        tv_title.setText("商品详情");
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected void initEvent() {

    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    private void getData() {
        OkHttpUtils.get().url(URL_PRODUCT + "/" + productsEntity.getProduct_id())
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                Status_Product status_product = GsonUtil.fromJson(response, Status_Product.class);
                if (CONNECTION_STATUS_SUCCESS.equals(status_product.getStatus_code())) {
                    productEntity = status_product.getData();
                    initBannerData();
                    wv_content.loadDataWithBaseURL("about:blank", productEntity.getContent().getContent(), "text/html", "utf-8", null);
                    tv_product_title.setText(productEntity.getTitle());
                    tv_point.setText(productEntity.getMoney());
                    tv_validity.setText(CommonUtils.TimeStamp2Date(productEntity.getValidity(), "yyyy-MM-dd"));
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(CommodityDetailActivity.this, status_error);
            }
        });
    }

    /**
     * 初始化banner数据
     */
    public void initBannerData() {
        mConvenientBanner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
            @Override
            public LocalImageHolderView createHolder() {
                return new LocalImageHolderView();
            }
        }, productEntity.getImages())
                .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(this);

        if (null != productEntity.getImages() && productEntity.getImages().size() <= 1) {
            mConvenientBanner.setCanLoop(false);
        } else {
            mConvenientBanner.setCanLoop(true);
        }
    }

    @OnClick(R.id.tv_buy)
    void buy() {
        startLocalActivity(this, PaymentActivity.class, PARAMS_SHOP_COMMODITY, productEntity);
    }

    @Override
    public void onItemClick(int position) {
    }

    @Override
    public void onResume() {
        super.onResume();
        mConvenientBanner.startTurning(3000);
    }

    // 停止自动翻页
    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        mConvenientBanner.stopTurning();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
